package com.carkeeper.shop.controller;

import com.carkeeper.entity.PageResult;
import com.carkeeper.entity.Result;
import com.carkeeper.entity.StatusCode;
import com.carkeeper.shop.pojo.Catalogue;
import com.carkeeper.shop.pojo.ServiceType;
import com.carkeeper.shop.service.CatalogueService;
import com.github.pagehelper.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/catalogue")
@Api(tags = "服务目录接口")
public class CatalogueController {

    @Autowired
    private CatalogueService catalogueService;

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/findAll/{page}/{size}")
    @ApiOperation("目录查询接口")
    private Result<Catalogue> findAll(@PathVariable("page")int page,@PathVariable("size") int size){

        Page<Catalogue> allPage = catalogueService.findAll(page, size);
        PageResult<Catalogue> cataloguePageResult = new PageResult<>(allPage.getTotal(), allPage.getResult());
        return new Result<>(true,StatusCode.OK,"",cataloguePageResult);
    }

    /**
     * 多条件分页查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/search/{page}/{size}")
    @ApiOperation("条件分页查询接口")
    private Result<PageResult<ServiceType>> findPage(@RequestParam Map<String,Object> searchMap, @PathVariable("page") int page, @PathVariable("size")int size){
        Page<Catalogue> termPage = catalogueService.findTermPage(searchMap, page, size);
        PageResult<Catalogue> cataloguePageResult = new PageResult<>(termPage.getTotal(), termPage.getResult());
        return new Result<>(true, StatusCode.OK,"查询成功",cataloguePageResult);
    }

    /**
     * 上下架服务目录
     * @param id
     * @return
     */
    @PutMapping("/update/{id}")
    @ApiOperation("上下架接口")
    private Result updateStatus(@PathVariable long id,@RequestParam String status){
        String message = catalogueService.updateStatus(id, status);
        return new Result(true,StatusCode.OK,"",message);
    }

    /**
     * 查看服务详细信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("查看服务详情接口")
    private Result<Map<String,Object>> details(@PathVariable("id") long id){
        Map<String, Object> detailsMap = catalogueService.details(id);
        return new Result<>(true,StatusCode.OK,"",detailsMap);
    }
}
